package com.itheima.youdianda.controller;


import com.itheima.youdianda.common.R;
import com.itheima.youdianda.domain.SysConfig;
import com.itheima.youdianda.dto.SysConfigDto;
import com.itheima.youdianda.dto.SysConfigTagDto;
import com.itheima.youdianda.service.SysConfigService;
import com.itheima.youdianda.utils.JwtUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author Pan Rundong
 * @Date 2023-02-13 20:53
 * @Desc 系统配置Controller
 */
@Slf4j
@RestController
@RequestMapping("/sys_config")
@Api(tags = "系统配置接口")
public class SysConfigController {
    @Autowired
    private SysConfigService sysConfigService;

    /**
     * 添加系统配置
     * @Author: Pan Rundong
     * @Date: 2023-02-13 21:04
     * @Greet2u:  （O｀ 3′O）~~♡
     * @param token
     * @param sysConfig
     * @return com.itheima.youdianda.common.R<java.lang.String>
     */
    @PostMapping("/add")
    @ApiOperation("系统配置添加")
    public R<String> save(@RequestHeader("x-token") String token, @RequestBody SysConfig sysConfig) {
        //验证token
        boolean flag = JwtUtils.verifyToken(token);
        if (flag) {
            sysConfigService.save(sysConfig);
            return R.success("系统配置设置成功");
        }
        return R.error("系统配置设置失败");
    }

    /**
     * 获取全部配置
     * @Author: Pan Rundong
     * @Date: 2023-02-13 21:27
     * @Greet2u:  （O｀ 3′O）~~♡
     * @param token
     * @return com.itheima.youdianda.common.R<java.util.List < com.itheima.youdianda.dto.SysConfigTagDto>>
     */
    @GetMapping("/all")
    @ApiOperation("获取全部配置")
    public R<SysConfigDto> getAll(@RequestHeader("x-token") String token) {
        //验证token
        boolean flag = JwtUtils.verifyToken(token);
        if (flag) {
            SysConfigDto sysConfigDto = new SysConfigDto();
            sysConfigDto.setList(sysConfigService.getAll2());
            sysConfigDto.setAllData(sysConfigService.getAllData());
            sysConfigDto.setConfigForm(sysConfigService.getAttrValue());
            sysConfigDto.setConfigRules(sysConfigService.getRules());


            return R.success(sysConfigDto,"获取系统配置成功");
        }
        return R.error("获取系统配置失败");
    }
}
